const app = getApp(),url = app.globalData.url;
function sizeTostr(size) {
  console.log('size',size)
  var data = "";
  if (size < 0.1 * 1024) { //如果小于0.1KB转化成B  
      data = size.toFixed(2) + "B";
  } else if (size < 0.1 * 1024 * 1024) {//如果小于0.1MB转化成KB  
      data = (size / 1024).toFixed(2) + "KB";
  } else if (size < 0.1 * 1024 * 1024 * 1024) { //如果小于0.1GB转化成MB  
      data = (size / (1024 * 1024)).toFixed(2) + "MB";
  } else { //其他转化成GB  
      data = (size / (1024 * 1024 * 1024)).toFixed(2) + "GB";
  }
  var sizestr = data + "";
  var len = sizestr.indexOf("\.");
  var dec = sizestr.substr(len + 1, 2);
  if (dec == "00") {//当小数点后为00时 去掉小数部分  
      return sizestr.substring(0, len) + sizestr.substr(len + 3, 2);
  }
  return sizestr;
}  
Page({

  data: {
    startY: 0,     //滑动开始y轴位置
    lastY: 0,     //滑动开始y轴位置
    showSearch: true, //是否显示搜索框
    nickName:'',
    fileUrl:'',
    nodel:true,
    readOnly: false // 是否只读
  },
// 监听页面滚动事件
handletouchmove (event) {
  let currentY = event.changedTouches[0].clientY
  if (currentY <= this.data.startY) {
    this.setData({
      showSearch: false
    })
    console.log("上滑")
  } else {
    this.setData({
      showSearch: true
    })
    console.log("下滑")
  }
},
//滑动开始事件
handletouchstart: function (event) {
  this.data.startY = event.changedTouches[0].clientY
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      readOnly: options.type === 'view',
      userUid: options.uid
    })
    var that = this
    let userUid = ''
    if(this.data.readOnly){
      userUid = options.uid
    } else {
      userUid = wx.getStorageSync('user_massage').userUid
    }
    if(userUid){
      wx.request({
        url: url + 'cvBaseInfo/getCVBaseInfoByUserUid?user_uid='+ userUid,
        method: "GET",
        success:function(res){
          console.log(" 姓名",res);
          var ret = res.data
            that.setData({
              nickName:ret.data.name,
            })
          }
      })
    }
      
    wx.request({
      url:url+ 'cvFile/getCVFile',
      method: "GET",
      data:{user_uid: userUid},
      success:function(res){
        console.log("简历附件",res);
        var ret = res.data
        if(ret.code == 200){
          wx.hideLoading()
          that.setData({
            id:ret.data.id,
            // fileSize:ret.data.fileSize,
            cvSuffix:ret.data.cvSuffix,
            cvFileName:ret.data.cvFileName,
            fileUrl:ret.data.fileUrl,
          })
          var size = sizeTostr(ret.data.fileSize)
          // var size = sizeTostr('11111')
          that.setData({
             fileSize:size,
          })
        }else{
          wx.hideLoading()
          that.setData({fileUrl:''})
          wx.showToast({
            title: '您还未上传简历附件',
            icon:'none'
          })
        }
      }
    })
  },
  look(e){
    console.log(e)
    var url = e.currentTarget.dataset.url//简历连接
    wx.downloadFile({
      url: url,
      success: function (res) {
        console.log(res)
        var Path = res.tempFilePath //返回的文件临时地址，用于后面打开本地预览所用
        wx.openDocument({
          filePath: Path,
          success: function (res) {
            console.log('打开文档成功')
          }
        })
      },
      fail:function (res){
        console.log(res)
      }
    })
  },
  del(e){
    var id = e.currentTarget.dataset.id//简历连接
    console.log(id)
    wx.request({
      url: url + 'cvFile/deleteCVFile?cv_file_id='+id,
      method: "get",
      success:function(res){
        console.log("删除经历",res);
        var ret = res.data
        if(ret.code == 200){
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1000)
          
        }else{
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })
  },
  onShow: function () {

  },

  delcheat(){
    var that = this
    that.setData({nodel:false})
  },
  over(){
    var that = this
    that.setData({nodel:true})
  },
})